<?php

session_start();
// Define path to application directory
defined('APPLICATION_PATH')
        || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
        || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
            realpath(APPLICATION_PATH . '/../library'),
            get_include_path(),
        )));

require_once 'Zend/Loader/Autoloader.php';
require_once '../library/krumo/class.krumo.php';
Zend_Loader_Autoloader::getInstance();


/** Zend_Application */
// Define path to application directory
defined('APPLICATION_PATH')
        || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
        || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
            realpath(APPLICATION_PATH . '/../library'),
            get_include_path(),
        )));

require_once 'Zend/Loader/Autoloader.php';
if (!isset($_SESSION['lang'])) {
    $_SESSION['lang'] = 'fr';
}
if (!isset($_SESSION['config'])) {
    $_SESSION['config'] = 'ini';
}
Zend_Loader_Autoloader::getInstance();
$ResourceAutoloader = new Zend_Loader_Autoloader_Resource(array(
    'basePath' => APPLICATION_PATH,
    'namespace' => 'My',
            'resourceTypes' => array(
                'form' => array(
                    'path' => 'forms/',
                    'namespace' => 'Form',
                )
            )
        )
);

/** Zend_Application */
$config = $_SESSION['config'];
switch ($config) {
    case 'php':
        $sConfigPath = APPLICATION_PATH . "/configs/application.php";
        $oConfig = new Zend_Config(require $sConfigPath);
        break;
    case "json":
        $sConfigPath = APPLICATION_PATH . "/configs/application.json";
        $oConfig = new Zend_Config_Json($sConfigPath, APPLICATION_ENV);
        break;
    case "xml":
        $sConfigPath = APPLICATION_PATH . "/configs/application.xml";
        $oConfig = new Zend_Config_Xml($sConfigPath, APPLICATION_ENV);
        break;
    case "yaml":
        $sConfigPath = APPLICATION_PATH . "/configs/application.yml";
        $oConfig = new Zend_Config_Yaml($sConfigPath, APPLICATION_ENV);
        break;
    case "ini":
    default :
        $sConfigPath = APPLICATION_PATH . "/configs/application.ini";
        $oConfig = new Zend_Config_Ini($sConfigPath, APPLICATION_ENV);
        break;
}

//var_dump($oConfig);
// Création d'un objet de base de donnée permettant d'utiliser la base de données
$oDb = Zend_Db::factory($oConfig->resources->db);

// Enregistrement de l'objet de connexion dans un conteneur de type registre dans l'espace d'application
// Ce mécanisme est une alternative à l'utilisation du stockage global
// Note : le même objet sera toujours disponible à tout niveau de l'application
$oDb->getConnection();

// BD par défaut
Zend_Db_Table::setDefaultAdapter($oDb);

/* * ******************************************************* */

//Chargement des classes du controlleer frontal 
Zend_Loader::loadClass('Zend_Controller_Front');

try {
    // récupération d'une instance du contrôleur frontal
    $oFront = Zend_Controller_Front::getInstance();
    // capacité à gérer les exceptions 
    $oFront->throwExceptions(true);
    // Ajout d'un répertoire de localisation pour le chargement des classes de controller d'action
    $oFront->addModuleDirectory(APPLICATION_PATH . '/modules/');
    $oFront->setControllerDirectory(array(
        'default' => APPLICATION_PATH . '/controllers',
        'game' => APPLICATION_PATH . '/modules/game/controllers',
            )
    );

    // Lancement de l'application
    Zend_Layout::startMvc(array(
        'layoutPath' => APPLICATION_PATH . '/layouts/scripts',
    ));
    
    $oFront->dispatch();
} catch (Exception $e) {
    include 'errors/404.phtml'; // traitement des exceptions du controler 
    print $e->getMessage();
} catch (Exception $e) {
    include 'errors/500.phtml'; // traitement des autres exceptions
    print $e->getMessage();
}
